///
/// Click handler for Button that uploads a file to a Data subfolder which exists
/// in the current application directory.
///
public override void ${Upload Button Control}_Click(Object sender, EventArgs args)
{
System.Web.UI.HtmlControls.HtmlInputFile inputFile;
inputFile = ((System.Web.UI.HtmlControls.HtmlInputFile)(this.Page.FindControlRecursively("inputFile")));
// If this code customization was added in Table Control Row, replace FindControlRecursively() with FindControl()
// or comment out the above line of code and uncomment the line below
// inputFile = ((System.Web.UI.HtmlControls.HtmlInputFile)(this.FindControl("inputFile")));
if ((!(inputFile.PostedFile == null) && (inputFile.PostedFile.ContentLength > 0)))
{
// Get the name of the file to be uploaded and
// the location where the file needs to be saved.
string fileName = System.IO.Path.GetFileName(inputFile.PostedFile.FileName);
string saveLocation = (this.Page.Server.MapPath("..\\Data") + ("\\" + fileName));
try
{
// Save the file.
inputFile.PostedFile.SaveAs(saveLocation);
this.Page.Response.Write("The file has been uploaded.");
}
catch (Exception ex)
{
this.Page.Response.Write(("Error: " + ex.Message));
}
}
else
{
this.Page.Response.Write("Please select a file to upload.");
}
}
|